package com.hoshiibuy.organization.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.organization.entity.ComOrganizationAttach;
import com.hoshiibuy.organization.entity.param.ComOrganizationAttachQueryParam;
import com.hoshiibuy.organization.entity.param.ComOrganizationAttachUpdateParam;
import com.hoshiibuy.organization.entity.param.ComOrganizationAttachAddParam;
import com.hoshiibuy.organization.service.ComOrganizationAttachService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiibuy.common.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 机构附件com_organization_attach 控制器
 * </p>
 *
 * @author campchen
 * @since 2019-04-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/com-organization-attach")
@Api(value = "供应商信息", tags = {"租户机构附件com_organization_attach管理"})
public class ComOrganizationAttachController {
    @Autowired
    private ComOrganizationAttachService comOrganizationAttachService;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询机构附件com_organization_attach
    */
    @ApiOperation(value = "分页查询机构附件com_organization_attach")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid ComOrganizationAttachQueryParam param) {
        try {
            log.info("分页查询机构附件com_organization_attach");
            QueryWrapper<ComOrganizationAttach> queryWrapper = new QueryWrapper<ComOrganizationAttach>();
            queryWrapper.eq(param.getComorgsId() != null, "comorgs_id", param.getComorgsId());
            queryWrapper.eq(param.getAttachId() != null, "attach_id", param.getAttachId());
            queryWrapper.eq(StringUtils.isNotBlank(param.getAttachUrl()), "attach_url", param.getAttachUrl());
            IPage<ComOrganizationAttach> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(ComOrganizationAttach.class));
            return Response.data(comOrganizationAttachService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询机构附件com_organization_attach异常", e);
            return Response.failed("分页查询机构附件com_organization_attach异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询机构附件com_organization_attach
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询机构附件com_organization_attach")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(comOrganizationAttachService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增机构附件com_organization_attach")
    public Response save(@RequestBody @Valid ComOrganizationAttachAddParam param) {
        try {
            ComOrganizationAttach data = BeanUtils.covertType(param, ComOrganizationAttach.class);
            return Response.data(comOrganizationAttachService.save(data));
        } catch (Exception e) {
            log.error("新增机构附件com_organization_attach异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改机构附件com_organization_attach
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改机构附件com_organization_attach")
    public Response update(@RequestBody @Valid ComOrganizationAttachUpdateParam param) {
        try {
            ComOrganizationAttach data = BeanUtils.covertType(param, ComOrganizationAttach.class);
            return Response.data(comOrganizationAttachService.updateById(data));
        } catch (Exception e) {
            log.error("修改机构附件com_organization_attach异常", e);
            return Response.failed();
        }
    }
}
